package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.system.domain.model.*;
import com.muyu.system.domain.req.SysNoticeAddReq;
import com.muyu.system.domain.req.SysNoticeListReq;
import com.muyu.system.domain.req.SysNoticeUpdReq;
import com.muyu.system.domain.resp.SysNoticeDetailResp;
import com.muyu.system.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告 信息操作处理
 *
 * @author muyu
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private SysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @RequiresPermissions("system:notice:list")
    @PostMapping("/list")
    public Result<DataPageResp<SysNoticeListModel>> list (@RequestBody SysNoticeListReq sysNoticeListReq) {
        PageQueryModel<SysNoticeListModel> sysNoticePageQueryModel = noticeService.pageQuery(SysNoticePageQueryModel.reqBuild(sysNoticeListReq));
        return Result.success(
                DataPageResp.of(sysNoticePageQueryModel)
        );
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @RequiresPermissions("system:notice:query")
    @GetMapping(value = "/{noticeId}")
    public Result<SysNoticeDetailResp> getInfo (@PathVariable("noticeId") Long noticeId) {
        SysNoticeDetailModel sysNoticeDetailModel = noticeService.selectNoticeById(noticeId);
        return Result.success(SysNoticeDetailResp.detailModelBuildToResp(sysNoticeDetailModel));
    }

    /**
     * 新增通知公告
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<String> add (@Validated @RequestBody SysNoticeAddReq sysNoticeAddReq) {
        noticeService.insertNotice(SysNoticeAddModel.addReqBuildToModel(sysNoticeAddReq,SecurityUtils::getUsername));
        return Result.success();
    }

    /**
     * 修改通知公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public Result<String> edit (@PathVariable("id") Long id,@Validated @RequestBody SysNoticeUpdReq sysNoticeUpdReq) {
        noticeService.updateNotice(id, SysNoticeUpdModel.updReqBuildToModel(sysNoticeUpdReq,SecurityUtils::getUsername));
        return Result.success();
    }

    /**
     * 删除通知公告
     */
    @RequiresPermissions("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public Result<String> remove (@PathVariable("noticeIds") List<Long> noticeIds) {
        noticeService.deleteNoticeByIds(noticeIds);
        return Result.success();
    }
}
